<?php


class ConfigSwitch 
{
    #constructor
    public function __construct($switchID, $dbConnection)
    {
        $this->Connection = $dbConnection;
        $this->ID = $switchID; 
        if($switchID != 0)
        {
        $this->getInformations();
        }
    }
    #end constructor
    
    #properties
    public $ID; 
    public $Connection;
    public $snmpPort;
    public $IP;
    public $Name; 
    public $Vlan;
    public $ChangeFlag;
    

    #end properties

    #methods
    public  function getInformations()
    {
        $vlans = array();
        $queries = new mysqlqueries();
        $switch = $this->Connection->query($queries->GetIndex($queries->Switch, $this->ID, null, null));
        if($rowSwitch = mysqli_fetch_array($switch)) 
        {
            $this->Name = $rowSwitch['name'];
            $this->IP = $rowSwitch['ip'];
            $this->ChangeFlag = $rowSwitch['changeFlag'];
            
            $vlanSwitch = $this->Connection->query($queries->GetIndex($queries->Switch_has_vlan, $this->ID, "switch_ID", null));  
            while ($rowVlanSwitch = mysqli_fetch_array($vlanSwitch)) 
            {
              
              $vlan = $this->Connection->query($queries->GetIndex($queries->Vlan, $rowVlanSwitch['vlan_ID'], null, null));
              while ($rowVlan = mysqli_fetch_array($vlan)) 
              {
                  array_push($vlans, $rowVlan);
              }
              $this->Vlan = $vlans;
            }
        }     
    }
    
    public function newSwitch($ipAddress, $sname)
    {
        $this->IP = $ipAddress;
        $this->Name = $sname;
        $queries = new mysqlqueries();
        
        $this->Connection->query($queries->Add($queries->Switch, array($sname, $ipAddress, 0, "")));
        $this->ID= $this->Connection->lastID();
        $this->getInformations();
    }
    
    public function newVlan(array $vlanName , array $vlanNumber)
    {
        $queries = new mysqlqueries();
        if(count($vlanName == count($vlanNumber)))
        {
            for($i=0; $i < count($vlanName); $i++)
            {
                $this->Connection->query($queries->Add($queries->Vlan, array($vlanName, $vlanNumber)));  
                $vlanID = $this->Connection->lastID();
                $this->Connection->query($queries->Add($queries->Switch_has_vlan, array($this->ID, $vlanID)));
            }
            $this->getInformations();
        }
    }
    
    public function UpdateSwitch($ipAddress, $sname)
    {        
       $queries = new mysqlqueries();
       $this->Connection->query($queries->Update($queries->Switch, array($sname, $ipAddress), $this->ID, array("name", "ip"), null));   
       
        $this->IP = $ipAddress;
        $this->Name = $sname;
    }
    
    public function removeVlan($vlanID)
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Remove($queries->Switch_has_vlan, array("switch_ID", "vlan_ID"), array($this->ID, $vlanID)));
        return true;
    }
    
    public function addVlan($vlanID)
    {
        $queries = new mysqlqueries();
        $resultVlan = $this->Connection->query($queries->GetIndex($queries->Switch_has_vlan, array($this->ID, $vlanID), array("switch_ID", "vlan_ID"), null));
        if($rowSwitch = mysqli_fetch_array($resultVlan)) 
        {
            return false;
        }
        $this->Connection->query($queries->Add($queries->Switch_has_vlan, array($this->ID, $vlanID)));
        return true;
    }
    
    public function deleteChangeFlag()
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Update($queries->Switch, "0", $this->ID, "changeFlag", null));
        return true;
        
    }
             
    #end methods      
}

